$(function () {
    createHtml();
    getSum();
    total();
    checkEvent();
    allCheck();
    numChange();
    removeEvent();
    balanceNow();

    /*插入HTML文档*/
    function createHtml() {
        var shopCarDatas = LsyStorage.getArr('shopCar');
        var str = '';
        for (var i = 0; i < shopCarDatas.length; i++) {
            var check = shopCarDatas[i].checked ? 'checked' : '';
            str += '<li data-id=' + shopCarDatas[i].id + '>'
                + '<label>'
                + '<input type="checkbox" ' + check + ' name="select">'
                + '<span></span>'
                + '</label>'
                + '<a href="pro_details.html?id='+shopCarDatas[i].id+'"><img src="' + shopCarDatas[i].imgSrc + '" alt="shop_cart_2.png"></a>'
                + '<div>'
                + '<div class="top">'
                + '<h3><a href="pro_details.html?id='+shopCarDatas[i].id+'">' + shopCarDatas[i].title + '</a></h3>'
                + '<b>¥<i>' + shopCarDatas[i].price + '</i></b>'
                + '<div class="num">'
                + '<img src="img/shop_cart_5.png" alt="shop_cart_5.png">'
                + '<input type="text" value="' + shopCarDatas[i].num + '">'
                + '<img src="img/shop_cart_6.png" alt="shop_cart_6.png">'
                + '</div>'
                + '<i>收藏</i>'
                + '</div>'
                + '<div class="bottom" id="bottom">'
                + '<b>编号：666<i>' + shopCarDatas[i].id + '</i></b>'
                + '<i>本订单已免运费</i>'
                + '<img data-id=' + shopCarDatas[i].id + ' src="img/shop_cart_7.png" alt="shop_cart_7.png">'
                + '</div>'
                + '</div>'
                + '</li>'
        }
        $('#shopCar').html(str);
    }
    /*获取商品件数*/
    function getSum() {
        var sum = $('#sum');
        var num = $('#shopCar').find('li label input:checked').length;
        sum.text(num);
    }
    /*获取商品总价格*/
    function total() {
        var shopCarLi = $('#shopCar li');
        var money = $('#money i');
        var totalPrice = 0;
        var totalFare=0;
        var right_span = $('#right div:first-child span b');
        shopCarLi.each(function (key, item) {
            var bool = $(item).find('label input').prop("checked");
            if (bool) {
                var price = $(item).find('.top b i').text();
                var num = $(item).find('.num input').val();
                totalPrice += price * num;
            }
        });
        totalPrice = totalPrice.toFixed(2);
        if(totalPrice>0 && totalPrice<100){
            totalFare=parseFloat(totalPrice)+30;
            totalFare=totalFare.toFixed(2);
        }else {
            totalFare=totalPrice;
        }
        money.text(totalFare);
        right_span.text(totalPrice);
    }
    /*给选择框添加代理事件*/
    function checkEvent() {
        $('#shopCar').on('click', 'input[type=checkbox]', function () {
            /*if($(this).prop('checked')){
             }*/
            getSum();
            total();
        })
    }
    /*全选*/
    function allCheck() {
        var balance_inp = $('#balance label input');
        var details_inp = $('#shopCar li label input');
        balance_inp.click(function () {
            if ($(this).is(':checked')) {
                details_inp.prop('checked', true);
            } else {
                details_inp.prop('checked', false);
            }
            getSum();
            total();
        })
    }
    /*产品数量改变*/
    function numChange() {
        var reduce = $('#shopCar li .num img:first-child');
        var plus = $('#shopCar li .num img:last-child');
        var inputChange = $('#shopCar li .num input');
        var numInp=$('#shopCar li .num input');
        numInp.keyup(function(){
            var textInp=$(this).val();
            textInp=integer(textInp);
            $(this).val(textInp);
            total();
        });
        numInp.blur(function(){
            var textInp=$(this).val();
            if(!textInp){
                textInp=1;
            }
            $(this).val(textInp);
        });
        plus.click(function () {
            var num = $(this).parents('.num').find('input').val();
            var id = $(this).parents('li').attr('data-id');
            var numInput = $(this).parents('.num').find('input');
            num++;
            numInput.val(num);
            total();
        });
        reduce.click(function () {
            var num = $(this).parents('.num').find('input').val();
            var id = $(this).parents('li').attr('data-id');
            var numInput = $(this).parents('.num').find('input');
            num--;
            if (num == 0) {
                num = 1;
            }
            numInput.val(num);
            total();
        });
        inputChange.keyup(function () {
            var num = $(this).val();
            var id = $(this).parents('li').attr('data-id');
            total();
        });
        inputChange.blur(function () {
            var num = $(this).val();
            if (num == 0) {
                num = 1;
            }
            $(this).val(num);
            total();
        })
    }
    /*删除商品*/
    function removeEvent() {
        var remove = $('#bottom img');
        var removeAll = $('#balance i');
        $('#shopCar').on('click', '#bottom img', function () {
            var shopCarId = $(this).attr('data-id');
            var bool = confirm("您确定要删除该商品");
            if (bool) {
                LsyStorage.removeItem('shopCar_' + shopCarId);
                $(this).parents('li').remove();
            }
            getSum();
            total();
        });
        removeAll.click(function () {
            var shopCarInp = $('#shopCar li label input:checked').parents('li');
            var boole = confirm("您确定要删除所选中的商品");
            if (boole) {
                for (var i = 0; i < shopCarInp.length; i++) {
                    var id = shopCarInp.eq(i).attr('data-id');
                    LsyStorage.removeItem('shopCar_' + id);
                }
                shopCarInp.remove();
            }
            getSum();
            total();
        })
    }
    /*立即结算*/
    function balanceNow() {
        var set = $('#balance b');
        var userName=LsyCookie.get('loginUser');
        set.click(function () {
            var buyNum=$('#money i').text();
            buyNum=Number(buyNum);
            if(buyNum){
                if(userName){
                    LsyStorage.removeArr('buyList');
                    var checkLi = $('#shopCar input[type=checkbox]:checked').parents('li');
                    checkLi.each(function(key,item){
                        var id=$(item).attr('data-id');
                        var buyShopDatas=LsyStorage.getItem('shopCar_'+id);
                        var num=$(item).find('input[type=text]').val();
                        buyShopDatas.num=num;
                        LsyStorage.setItem('buyList_'+id,buyShopDatas);
                    });
                    window.location.href='confirm_order.html';
                }else {
                    window.location.href='login.html';
                }
            }
        })
    }
});

window.onunload = function () {
    var shopCarLi = $('#shopCar li');
    for (var i = 0; i < shopCarLi.length; i++) {
        var id = shopCarLi.eq(i).attr('data-id');
        var bool = shopCarLi.eq(i).find('input[type=checkbox]').prop('checked');
        var shopCarDatas = LsyStorage.getItem('shopCar_' + id);
        var num = shopCarLi.eq(i).find('.num input').val();
        shopCarDatas.num = num;
        if (bool) {
            shopCarDatas.checked = true;
        } else {
            shopCarDatas.checked = false;
        }
        LsyStorage.setItem('shopCar_' + id, shopCarDatas);
    }
};
